package com.mars.upms.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mars.base.common.base.BaseController;
import com.mars.base.common.base.BaseResult;
import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.Paginator;
import com.mars.upms.dto.UpmsUserDto;
import com.mars.upms.entity.UpmsGroup;
import com.mars.upms.rpc.api.UpmsGroupService;

@RestController
@RequestMapping(value = "/api/upms/group")
public class UpmsGroupController extends BaseController {

	@Autowired
	private UpmsGroupService upmsGroupService;

	@PostMapping(value = "/add")
	public Object add(@RequestBody UpmsGroup entity) {
		try {
			if (StringUtils.isBlank(entity.getName())) {
				return BaseResult.failure("角色名称不能为空");
			}
			this.upmsGroupService.insertSelective(entity);

			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		try {
			this.upmsGroupService.delete(id);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@PostMapping(value = "/update")
	public Object update(@RequestBody UpmsGroup entity) {
		try {
			if (StringUtils.isBlank(entity.getName())) {
				return BaseResult.failure("角色名称不能为空");
			}
			this.upmsGroupService.updateSelective(entity);

			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@GetMapping(value = "/get/{id}")
	public Object get(@PathVariable Integer id) {
		return this.upmsGroupService.selectById(id);
	}

	@GetMapping(value = "/list")
	public Object list(UpmsGroup entity) {
		return this.upmsGroupService.selectPage(new Paginator(this.request), entity);
	}
	
	/**
	 * 查询组用户列表
	 * @param dto
	 * @return
	 */
	@GetMapping(value = "/group_user_list")
	public Object groupUserList(UpmsUserDto dto) {
		return this.upmsGroupService.getGroupUserAssignList(new Paginator(this.request), dto);
	}
	
	/**
	 * 将用户移出组
	 * @param groupAssingId
	 * @return
	 */
	@GetMapping(value = "/remove_group_user")
	public Object removeGroupUser(Integer groupAssingId) {
		try {
			this.upmsGroupService.removeGroupUser(groupAssingId);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}
}
